/*
*  Copyright 2019-2025 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.glls.edu.service.impl;

import com.glls.edu.domain.Student;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.glls.edu.service.StudentService;
import com.glls.edu.domain.dto.StudentQueryCriteria;
import com.glls.edu.mapper.StudentMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author glls
* @date 2025-03-29
**/
@Service
@RequiredArgsConstructor
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    private final StudentMapper studentMapper;

    @Override
    public PageResult<Student> queryAll(StudentQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(studentMapper.findAll(criteria, page));
    }

    @Override
    public List<Student> queryAll(StudentQueryCriteria criteria){
        return studentMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(Student resources) {
        studentMapper.insert(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Student resources) {
        Student student = getById(resources.getId());
        student.copy(resources);
        studentMapper.updateById(student);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Integer> ids) {
        studentMapper.deleteBatchIds(ids);
    }

    @Override
    public void download(List<Student> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (Student student : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("学号", student.getStuNo());
            map.put("姓名", student.getStuName());
            map.put("性别", student.getGender());
            map.put("电话", student.getPhone());
            map.put("邮箱", student.getEmail());
            map.put("专业", student.getMajor());
            map.put("班级", student.getClazz());
            map.put("学校", student.getSchool());
            map.put("照片", student.getPic());
            map.put("生日", student.getBirthday());
            map.put("家庭住址", student.getAddress());
            map.put("创建时间", student.getCreateTime());
            map.put("修改时间", student.getUpdateTime());
            map.put("兴趣爱好", student.getHobby());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}